package Forms;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.BevelBorder;
import Logic.DatabaseReader;

public class DatabaseConnector extends JFrame {

	private JPanel contentPane;
	private JTextField urlTextField;
	private JTextField userTextField;
	private JPasswordField passwordField;
	private JTextField dbNameField;
	private JSpinner spinnerPort;
	private DatabaseReader databaseReader;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame. As the UI shows this was done really fast, just so the
	 * program is even more dynamical as its name suggests this is for
	 * connecting the software to a database
	 */
	public DatabaseConnector(DatabaseReader _databaseReader) {
		setTitle("Database Connector");
		databaseReader = _databaseReader;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[109px][315px,grow]",
				"[30px][189px,grow][33px]"));

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));
		panel.setBackground(new Color(175, 238, 238));
		contentPane.add(panel, "cell 0 0 2 1,growx,aligny top");

		JLabel lblUrl = new JLabel("Url: ");
		panel.add(lblUrl);

		urlTextField = new JTextField();
		panel.add(urlTextField);
		urlTextField.setColumns(30);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, "cell 0 1,alignx left,growy");
		panel_1.setLayout(new MigLayout("", "[24px][31px,grow][76px]",
				"[22][22][][][][][140px][][][][23px]"));

		JLabel lblPort = new JLabel("Port:");
		panel_1.add(lblPort, "cell 1 1,alignx left,aligny center");

		spinnerPort = new JSpinner();
		spinnerPort.setPreferredSize(new Dimension(70, 20));
		spinnerPort.setModel(new SpinnerNumberModel(new Integer(0),
				new Integer(0), null, new Integer(1)));
		panel_1.add(spinnerPort, "cell 1 2,alignx center,aligny center");

		JLabel lblDatabaseName = new JLabel("Database Name");
		panel_1.add(lblDatabaseName, "cell 1 4,alignx left,aligny center");

		dbNameField = new JTextField();
		panel_1.add(dbNameField, "cell 1 5,growx");
		dbNameField.setColumns(10);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(null);
		panel_3.setBackground(Color.LIGHT_GRAY);
		contentPane.add(panel_3, "cell 1 1,grow");
		panel_3.setLayout(new MigLayout("", "[52px][86px][grow]",
				"[20px][][][]"));

		JLabel lblUsername = new JLabel("Username:");
		panel_3.add(lblUsername, "cell 1 2,alignx left,aligny center");

		userTextField = new JTextField();
		panel_3.add(userTextField, "cell 2 2,growx");
		userTextField.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		panel_3.add(lblPassword, "cell 1 3");

		passwordField = new JPasswordField();
		passwordField.setHorizontalAlignment(SwingConstants.LEFT);
		passwordField.setColumns(10);
		panel_3.add(passwordField, "cell 2 3,growx");

		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, "cell 0 2 2 1,growx,aligny top");

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		panel_2.add(btnCancel);

		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (checkFields()) {
					JOptionPane.showMessageDialog(getContentPane(),
							databaseReader.setDataBaseCredentials(urlTextField
									.getText(), (int) spinnerPort.getValue(),
									dbNameField.getText(), userTextField
											.getText(), new String(
											passwordField.getPassword())));
					if (databaseReader.isDatabaseConnected()) {
						dispose();
					}
				}
			}
		});
		panel_2.add(btnConnect);
		setVisible(true);

	}

	public boolean checkFields() {

		if (urlTextField.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Url missing!");
			return false;
		}
		if (dbNameField.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Database name missing!");
			return false;
		}
		if (userTextField.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "Username missing!");
			return false;
		}

		return true;
	}

}
